import 'dart:async';

import 'package:flutter_bloc/flutter_bloc.dart';

class CounterBloc2 extends Bloc<UpateCounterEvent, CounterState> {
  StreamController<int> _streamController = StreamController();

  int _count = 0;

  late StreamSink<int> _sink;

  init() {
    _sink = _streamController.sink;
    _streamController.stream.listen((event) {
      add(UpateCounterEvent.UPDATE);
    });
  }

  CounterBloc2() : super(CounterState((0)));

  increment() {
    _count++;
    _sink.add(_count);
    // add(UpateCounterEvent.UPDATE);
  }

  @override
  Stream<CounterState> mapEventToState(UpateCounterEvent event) async* {
    switch (event) {
      case UpateCounterEvent.UPDATE:
        yield CounterState(_count);
        break;
    }
  }
}

enum UpateCounterEvent { UPDATE }

class CounterState {
  final int count;

  CounterState(this.count);
}
